<?php

//require_once 'object.php';
namespace bee;

define("GR_TYPE_PIE", 1);
define("GR_TYPE_BAR", 2);
define("GR_TYPE_BAR_ADD", 130);

/**
 * klasa grafa
 * @author dzelenika
 */
class graph extends object
{	public $gr_type;
	public $graph_contrast;
	public $graph_lightness;
	public $background_jpg;
	public $caption;
	public $elevation;
	public $thicknes;

	public $line_thickkness;
	public $graph_width;
	public $graph_height;
	public $border_height;
	public $border_width;
	public $margin;
	public $overrun;

	public $labels;

	public	$source;
	private $agg_source;
	private $size;									// veličina grafa
	private $data_sum;
	private $colors;
	private $colors_count;
	private $im;
	private $max_selem;
	private $left;
	private $bottom;
	//private $zero;
	private $min;
	private $max;
	//private $k;
	//private $bar_step;



	public function setSource($array)
	{	$this->source = array();
		$this->agg_source = array();
		$this->min = 0;
		$this->max = 0;

		if(!is_array($array))
		{	$array = array($array);
		}
		//print_r($array);die();
		foreach ($array as $akey => $avalue)
		{ if (!is_array($avalue))
			{	$avalue = array($akey => $avalue);
			}
			$this->source[$akey] = $avalue;
			$sum = 0;
			foreach ($avalue as $value)
			{	$sum += $value;
			}
			if(count($avalue) > $this->max_selem) $this->max_selem = count($avalue);
			$this->agg_source[$akey] = $sum;

			$minar = min($avalue);
			$maxar = max($avalue);
			//echo '<br />' . print_r($maxar, true) . '//' . print_r($minar, true);
			if($minar < $this->min)	$this->min = $minar;
			if($maxar > $this->max) $this->max = $maxar;
		}
		$this->min = ($this->min > 0) ? 0 : $this->min;
		$this->max = ($this->max < 0) ? 0 : $this->max;


	}
	public function setSize($size)
	{	$this->size = $size;
		$this->left = $this->graph_width / 2 - $this->size / 2;
		$this->bottom = $this->graph_height / 2 + $this->size / 2;

	}

	public function barStep()
	{	return $this->size / count($this->source);
	}
	public function barWidth()
	{	return $this->bar_step * (1 - $this->margin / 2);
	}
	public function k()
	{	if ($this->max != $this->min)
		{	return  $this->size / ($this->max - $this->min);
		}	else
		{	return $this->size;
		}
	}
	public function zero()
	{	return $this->bottom + $this->k() * $this->min;

	}

	function __construct($width, $height)
	{	$this->graph_width 		= $width;
		$this->graph_height 	= $height;
		$this->im = imagecreatetruecolor($this->graph_width, $this->graph_height);

		//
		$this->colors[] = imagecolorallocate($this->im, 255, 0, 0);
		$this->colors[] = imagecolorallocate($this->im, 0, 255, 0);
		$this->colors[] = imagecolorallocate($this->im, 0, 0, 255);
		$this->colors[] = imagecolorallocate($this->im, 128, 128, 128);
		$this->colors[] = imagecolorallocate($this->im, 64, 128, 255);
		$this->colors[] = imagecolorallocate($this->im, 255, 64, 128);
		$this->colors[] = imagecolorallocate($this->im, 128, 255, 64);
		$this->colors[] = imagecolorallocate($this->im, 255, 128, 64);
		$this->colors[] = imagecolorallocate($this->im, 64, 255, 128);
		$this->colors[] = imagecolorallocate($this->im, 128, 64, 255);
		$this->colors[] = imagecolorallocate($this->im, 255, 255, 0);
		$this->colors[] = imagecolorallocate($this->im, 255, 0, 255);
		$this->colors[] = imagecolorallocate($this->im, 0, 255, 255);
		$this->colors[] = imagecolorallocate($this->im, 255, 255, 255);
		$this->colors[] = imagecolorallocate($this->im, 64, 64, 64);
		$this->colors_count = 15;
		$this->setSize(500);
//		$this->elevation = .5;
//		$this->thicknes = 40;
//		$this->border_width = 5;
//		$this->border_height = 3;
//		$this->graph_lightness
			$this->graph_contrast = .3;
			$this->graph_lightness = .7;
			$this->border_height = 3;
			$this->border_width = 4;
			$this->elevation = .7;
			$this->thicknes = 40;

	}
	function showGraph($file = '')
	{	if($this->gr_type == GR_TYPE_PIE)
		{	$this->showPie();
		} elseif ($this->gr_type == GR_TYPE_BAR)
		{	$this->showBar();
		}	elseif ($this->gr_type == GR_TYPE_BAR_ADD)
		{	$this->showBarAdd();
		}
		if(!$file)
		{	header('Content-type: image/png');
		}
		imagepng($this->im, $file ? $file : null);


	}
	function showBar($file = '')
	{	//print_r($this);
		$bim     = imagecreatefromjpeg ($this->background_jpg);
		imagecopyresampled($this->im, $bim, 0, 0, 0, 0, imagesx($this->im), imagesy($this->im), imagesx($bim), imagesy($bim) );
		imagedestroy($bim);
		$black = imagecolorallocate($this->im, 0, 0, 0);
		$orange = imagecolorallocate($this->im, 220, 210, 60);
		$lorange = imagecolorallocate($this->im, 242, 231, 66);
		$dorange = imagecolorallocate($this->im, 200, 190, 54);

		//$bar_step = $this->size / count($this->source);

		//$bottom = $this->graph_height / 2 + $this->size / 2;
		//$left = $this->graph_width / 2 - $this->size / 2;

		imagepolygon($this->im,	array(	$this->left, $this->bottom,
																					$this->left, $this->bottom - $this->size,
																					$this->left - $this->thicknes * $this->overrun * sin($this->elevation) * $this->max_selem, $this->bottom - $this->size + $this->thicknes * $this->overrun * cos($this->elevation) * $this->max_selem,
																					$this->left - $this->thicknes * $this->overrun * sin($this->elevation) * $this->max_selem, $this->bottom + $this->thicknes * $this->overrun * cos($this->elevation) * $this->max_selem),
												4, $dorange);


		$counter = 0;
		foreach ($this->source as $avalue)
		{	$dcounter = 0;
			foreach ($avalue as $value)
			{	if($value < 0)
				{	$this->drawCube($counter, $dcounter, $value, $this->colors[$dcounter]);
				}
				$dcounter++;
			}
			$counter++;
		}
		for ($i=0;$i<$this->max_selem; $i++)
		{	imagepolygon($this->im,	array(	$this->left - $this->thicknes * $this->overrun * sin($this->elevation) * $i, $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * $i,
																			$this->left + $this->size - $this->thicknes * $this->overrun * sin($this->elevation) * $i, $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * $i,
																			$this->left + $this->size - $this->thicknes * $this->overrun * sin($this->elevation) * ($i + 1), $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * ($i + 1),
																			$this->left - $this->thicknes * $this->overrun * sin($this->elevation) * ($i + 1), $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * ($i + 1)),
												4, $orange);
		}
		$counter = 0;
		foreach ($this->source as $avalue)
		{	imagepolygon($this->im,	array(	$this->left + $counter * $this->barStep()- $this->thicknes * $this->overrun * sin($this->elevation) * 0, $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * 0,
																			$this->left + ($counter + 1) * $this->barStep() - $this->thicknes * $this->overrun * sin($this->elevation) * 0, $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * 0,
																			$this->left + ($counter + 1) * $this->barStep() - $this->thicknes * $this->overrun * sin($this->elevation) * $this->max_selem, $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * $this->max_selem,
																			$this->left + $counter * $this->barStep()- $this->thicknes * $this->overrun * sin($this->elevation) * $this->max_selem, $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * $this->max_selem),
												4, $orange);
			$counter++;
		}
		$counter = 0;
		foreach ($this->source as $avalue)
		{	$dcounter = 0;
			foreach ($avalue as $value)
			{	if($value > 0)
				{	$this->drawCube($counter, $dcounter, $value, $this->colors[$dcounter]);
				}
				$dcounter++;
			}
			$counter++;
		}

	}

	function showBarAdd($file = '')
	{	//print_r($this);
		$bim     = imagecreatefromjpeg ($this->background_jpg);
		imagecopyresampled($this->im, $bim, 0, 0, 0, 0, imagesx($this->im), imagesy($this->im), imagesx($bim), imagesy($bim) );
		imagedestroy($bim);
		$black = imagecolorallocate($this->im, 0, 0, 0);
		$orange = imagecolorallocate($this->im, 220, 210, 60);
		$lorange = imagecolorallocate($this->im, 242, 231, 66);
		$dorange = imagecolorallocate($this->im, 200, 190, 54);

		imagepolygon($this->im,	array(	$this->left, $this->bottom,
																					$this->left, $this->bottom - $this->size,
																					$this->left - $this->thicknes * $this->overrun * sin($this->elevation) * $this->max_selem, $this->bottom - $this->size + $this->thicknes * $this->overrun * cos($this->elevation) * $this->max_selem,
																					$this->left - $this->thicknes * $this->overrun * sin($this->elevation) * $this->max_selem, $this->bottom + $this->thicknes * $this->overrun * cos($this->elevation) * $this->max_selem),
												4, $dorange);

		for ($i=0;$i<$this->max_selem; $i++)
		{	imagepolygon($this->im,	array(	$this->left - $this->thicknes * $this->overrun * sin($this->elevation) * $i, $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * $i,
																			$this->left + $this->size - $this->thicknes * $this->overrun * sin($this->elevation) * $i, $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * $i,
																			$this->left + $this->size - $this->thicknes * $this->overrun * sin($this->elevation) * ($i + 1), $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * ($i + 1),
																			$this->left - $this->thicknes * $this->overrun * sin($this->elevation) * ($i + 1), $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * ($i + 1)),
												4, $orange);
		}
		$counter = 0;
		foreach ($this->source as $avalue)
		{	imagepolygon($this->im,	array(	$this->left + $counter * $this->barStep()- $this->thicknes * $this->overrun * sin($this->elevation) * 0, $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * 0,
																			$this->left + ($counter + 1) * $this->barStep() - $this->thicknes * $this->overrun * sin($this->elevation) * 0, $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * 0,
																			$this->left + ($counter + 1) * $this->barStep() - $this->thicknes * $this->overrun * sin($this->elevation) * $this->max_selem, $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * $this->max_selem,
																			$this->left + $counter * $this->barStep()- $this->thicknes * $this->overrun * sin($this->elevation) * $this->max_selem, $this->zero() + $this->thicknes * $this->overrun * cos($this->elevation) * $this->max_selem),
												4, $orange);
			$counter++;
		}
		$counter = 0;
		foreach ($this->source as $avalue)
		{	$dcounter = 0;
			foreach ($avalue as $value)
			{	if($value > 0)
				{	$this->drawCube($counter, $dcounter, $value, $this->colors[$dcounter]);
				}
			$dcounter++;
			}
		$counter++;
		}


	}
	function showPie($file = '')
	{	$bim     = imagecreatefromjpeg ($this->background_jpg);
		imagecopyresampled($this->im, $bim, 0, 0, 0, 0, imagesx($this->im), imagesy($this->im), imagesx($bim), imagesy($bim) );
		imagedestroy($bim);
		$black = imagecolorallocate($this->im, 0, 0, 0);
		$orange = imagecolorallocate($this->im, 220, 210, 60);
		$lorange = imagecolorallocate($this->im, 242, 231, 66);
		$this->data_sum = 0;
		
		foreach ($this->agg_source as $value)
		{	$this->data_sum += $value;
		}

		imagefilledellipse($this->im, 		$this->graph_width / 2,
																			$this->graph_height / 2,
																			$this->size,
																			$this->size * sin($this->elevation),
																			$black);

		imagefilledellipse($this->im, 		$this->graph_width / 2 ,
																			$this->graph_height / 2 - $this->border_height * cos($this->elevation),
																			$this->size - 2 * $this->border_width,
																			$this->size * sin($this->elevation) - 2 * $this->border_width * sin($this->elevation),
																			$black);

		for($i = $this->size / 2; $i >= -$this->size / 2; $i-=.9999)
		{
			$ang = acos($i / ($this->size / 2));
			$rgb = $this->colors[$this->ang2index($ang)];
			$rr = ($rgb >> 16) & 0xFF;
			$gg = ($rgb >> 8) & 0xFF;
			$bb = $rgb & 0xFF;

			if(($r = (int)($rr * ($this->graph_lightness - $i/300*$this->graph_contrast ))) > 255)
			{	$r = 255;
			}
			if($r < 0)
			{	$r = 0;	}

			if(($g = (int)($gg * ($this->graph_lightness - $i/300*$this->graph_contrast ))) > 255)
			{	$g = 255;
			}
			if($g < 0)
			{	$g = 0;
			}
			if(($b = (int)($bb * ($this->graph_lightness - $i/300*$this->graph_contrast ))) > 255)
			{	$b = 255;
			}
			if($b < 0)
			{ $b = 0;
			}

			$color = imagecolorallocate($this->im, $r, $g, $b);

			imageline($this->im, 	$this->graph_width / 2 + $this->size / 2 * cos($ang),
														$this->graph_height / 2 + $this->size / 2 * sin($this->elevation) * sin($ang),
														$this->graph_width / 2 + $this->size / 2 * cos($ang),
														$this->graph_height / 2 + $this->size / 2 * sin($this->elevation) * sin($ang) + $this->thicknes * cos($this->elevation),
														$color );

			imagecolordeallocate($this->im, $color);

		}

		$d = pi()/(300 * 100);

		$sum = 0;
		$counter = 0;
		if($this->data_sum)
		{		foreach ($this->agg_source as $value)
		{	$sum += $value;
			$ang =  round(360 * $sum / $this->data_sum);
			$ang2 = round(360 * ($sum - $value / 2) / $this->data_sum);
			$ang3 = round(360 * ($sum - $value) / $this->data_sum);
			$dbcolor = $this->BriCon($this->colors[$counter % $this->colors_count], -50, 1);
			$counter++;
		}
		$sum=0;
		$counter=0;
		imageline($this->im, 				$this->graph_width / 2,
																$this->graph_height / 2 - $this->border_height * cos($this->elevation),
																$this->graph_width / 2 + ($this->size / 2 - $this->border_width)  * cos(0 / 180 * pi()),
																$this->graph_height / 2 - $this->border_height * cos($this->elevation) + ($this->size / 2 * sin($this->elevation) - $this->border_width * sin($this->elevation)) * sin(0 / 180 * pi()),
																$dbcolor);
		imageline($this->im, 				$this->graph_width / 2 + ($this->size / 2 - $this->border_width) * cos(0 / 180 * pi()),
																$this->graph_height / 2 - $this->border_height * cos($this->elevation) + ($this->size / 2 * sin($this->elevation) - $this->border_width * sin($this->elevation) ) * sin(0 / 180 * pi()),
																$this->graph_width / 2 + $this->size / 2 * cos(0 / 180 * pi()),
																$this->graph_height / 2 + $this->size / 2 * sin($this->elevation) * sin(0 / 180 * pi()),
																$dbcolor);


		foreach ($this->agg_source as $value)
		{	$sum += $value;
			$ang =  floor(360 * $sum / $this->data_sum);
			$ang2 = floor(360 * ($sum - $value / 2)/$this->data_sum);
			$ang3 = floor(360 * ($sum - $value)/$this->data_sum);
			$lbcolor = $this->BriCon($this->colors[$counter % $this->colors_count], 100, 1);
			$dbcolor = $this->BriCon($this->colors[$counter % $this->colors_count], -50, 1);
//$dbcolor = imagecolorallocate($this->im, 250, 00, 0);
			$rgb = $this->colors[$counter % $this->colors_count];
			$rr = ($rgb >> 16) & 0xFF;
			$gg = ($rgb >> 8) & 0xFF;
			$bb = $rgb & 0xFF;

			if(($r = (int)($rr * ($this->graph_lightness - cos($ang2/180*pi()) * $this->graph_contrast ))) > 255)
			{	$r = 255;
			}
			if($r < 0)
			{	$r = 0;	}

			if(($g = (int)($gg * ($this->graph_lightness - cos($ang2/180*pi()) * $this->graph_contrast ))) > 255)
			{	$g = 255;
			}
			if($g < 0)
			{	$g = 0;
			}
			if(($b = (int)($bb * ($this->graph_lightness - cos($ang2/180*pi()) * $this->graph_contrast ))) > 255)
			{	$b = 255;
			}
			if($b < 0)
			{ $b = 0;
			}
			$color = imagecolorallocate($this->im, $r, $g, $b);
			$color = $this->BriCon($color, 80, 1);


			imageline($this->im, 			$this->graph_width / 2,
																$this->graph_height / 2 - $this->border_height * cos($this->elevation),
																$this->graph_width / 2 + ($this->size / 2 - $this->border_width)  * cos($ang / 180 * pi()),
																$this->graph_height / 2 - $this->border_height * cos($this->elevation) + ($this->size / 2 * sin($this->elevation) - $this->border_width * sin($this->elevation)) * sin($ang / 180 * pi()),
																$dbcolor);


			imageline($this->im, 			$this->graph_width / 2 + ($this->size / 2 - $this->border_width) * cos(($ang) / 180 * pi()),
																$this->graph_height / 2 - $this->border_height * cos($this->elevation) + ($this->size / 2 * sin($this->elevation) - $this->border_width * sin($this->elevation) ) * sin(($ang) / 180 * pi()),
																$this->graph_width / 2 + $this->size / 2 * cos(($ang) / 180 * pi()),
																$this->graph_height / 2 + $this->size / 2 * sin($this->elevation) * sin(($ang) / 180 * pi()),
																$dbcolor);

			imagefill($this->im, 			$this->graph_width / 2 + ($this->size / 2 - $this->border_width - 3) * cos($ang2 / 180 * pi()),
																$this->graph_height / 2 - $this->border_height * cos($this->elevation) + ($this->size / 2 * sin($this->elevation) - $this->border_width * sin($this->elevation) - 3) * sin($ang2 / 180 * pi()),
																$color);

			imagearc($this->im, 			$this->graph_width / 2,
																$this->graph_height / 2 - $this->border_height * cos($this->elevation),
																$this->size - 2 * $this->border_width,
																$this->size * sin($this->elevation) - 2 * $this->border_width * sin($this->elevation),
																$ang3, $ang,
																$dbcolor);
																//$this->colors[$counter % $this->colors_count]);

			imagefill($this->im, 			$this->graph_width / 2 + ($this->size / 2 - $this->border_width - 15) * cos($ang2 / 180 * pi()),
																$this->graph_height / 2 - $this->border_height * cos($this->elevation) + ($this->size / 2 * sin($this->elevation) - $this->border_width * sin($this->elevation) - 15) * sin($ang2 / 180 * pi()),
																//$this->colors[$counter % $this->colors_count]);
																$dbcolor);

			imagearc($this->im, 			$this->graph_width / 2,
																$this->graph_height / 2 - $this->border_height * cos($this->elevation),
																$this->size - 2 * $this->border_width,
																$this->size * sin($this->elevation) - 2 * $this->border_width * sin($this->elevation),
																$ang3, $ang,
																$dbcolor);
																//$this->colors[$counter % $this->colors_count]);
																//if($counter>6)break;

			$counter++;

		}
		$sum=0;
		$counter = 0;
		//print_r($this->agg_source);
		//print_r($this->labels);
		//if($this->data_sum)
		foreach ($this->agg_source as $key => $value)
		{	$sum += $value;
			$ang =  floor(360 * $sum / $this->data_sum);
			$ang2 = floor(360 * ($sum - $value / 2)/$this->data_sum);
			$ang3 = floor(360 * ($sum - $value)/$this->data_sum);
			imagestring($this->im, 3, $this->graph_width / 2 + .75 * $this->size / 2 * cos($ang2/180*pi()) + 3,
								$this->graph_height / 2 + .75 * $this->size / 2 * sin($this->elevation) * sin($ang2/180*pi()) - 6, @$this->labels[$key], //. '(' . @$value . ')',
								$lorange);
			/*
			imageline(		$this->im, $this->graph_width/2 + .75 * 300 * cos($ang2),
										$this->graph_height/2+ .75 * 200 * sin($ang2)
										,$this->graph_width/2 + .75 * 300 * cos($ang2) + 30,
										$this->graph_height/2+ .75 * 200 * sin($ang2) - 50, $orange);

			*/
								$counter++;
		}
		}
		//imagedestroy($this->im);
	}
	private function drawCube($ord_nr, $ser_nr, $value, $color, $bvalue = 0)
	{	if($bvalue > $value)
		{	$pom = $bvalue;
			$bvalue = $value;
			$value = $pom;
		}
		$dcolor = $this->BriCon($color, -50, 1);
		$lcolor = $this->BriCon($color, 50, 1);

		imagefilledrectangle($this->im, 			$this->left + $this->barStep() * ($ord_nr + $this->margin) - $this->thicknes * sin($this->elevation) * (.5 + (.5 + $ser_nr) * $this->overrun),
																					$this->zero() - $this->k() * $bvalue + $this->thicknes * cos($this->elevation) * (.5 + (.5 + $ser_nr) * $this->overrun),
																					$this->left + $this->barStep() * ($ord_nr + 1 - $this->margin) - $this->thicknes * sin($this->elevation) * (.5 + (.5 + $ser_nr) * $this->overrun),
																					$this->zero() - $this->k() * $value + $this->thicknes * cos($this->elevation) * (.5 + (.5 + $ser_nr) * $this->overrun),
																					$lcolor);

		imagefilledpolygon($this->im, array(	$this->left + $this->barStep() * ($ord_nr + 1 - $this->margin) - $this->thicknes * sin($this->elevation) * (-.5 + (.5 + $ser_nr) * $this->overrun), $this->zero() - $this->k() * $bvalue + $this->thicknes * cos($this->elevation) * (-.5 + (.5 + $ser_nr) * $this->overrun),
																					$this->left + $this->barStep() * ($ord_nr + 1 - $this->margin) - $this->thicknes * sin($this->elevation) * (-.5 + (.5 + $ser_nr) * $this->overrun), $this->zero() - $this->k() * $value + $this->thicknes * cos($this->elevation) * (-.5 + (.5 + $ser_nr) * $this->overrun),
																					$this->left + $this->barStep() * ($ord_nr + 1 - $this->margin) - $this->thicknes * sin($this->elevation) * (.5 + (.5 + $ser_nr) * $this->overrun), $this->zero() - $this->k() * $value + $this->thicknes * cos($this->elevation) * (.5 + (.5 + $ser_nr) * $this->overrun),
																					$this->left + $this->barStep() * ($ord_nr + 1 - $this->margin) - $this->thicknes * sin($this->elevation) * (.5 + (.5 + $ser_nr) * $this->overrun), $this->zero() - $this->k() * $bvalue + $this->thicknes * cos($this->elevation)* (.5 + (.5 + $ser_nr) * $this->overrun) ),
													4, $dcolor);
		if($bvalue>= 0 || 99)
		{	imagefilledpolygon($this->im, array($this->left + $this->barStep() * ($ord_nr + 1 - $this->margin) - $this->thicknes * sin($this->elevation) * (-.5 + (.5 +  $ser_nr) * $this->overrun), $this->zero() - $this->k() * $value + $this->thicknes * cos($this->elevation) * (-.5 + (.5 + $ser_nr) * $this->overrun),
																					$this->left + $this->barStep() * ($ord_nr + 1 - $this->margin) - $this->thicknes * sin($this->elevation) * (.5 + (.5 +  $ser_nr) * $this->overrun), $this->zero() - $this->k() * $value + $this->thicknes * cos($this->elevation) * (.5 + (.5 + $ser_nr) * $this->overrun),
																					$this->left + $this->barStep() * ($ord_nr + $this->margin) - $this->thicknes * sin($this->elevation) * (.5 + (.5 + $ser_nr) * $this->overrun), $this->zero() - $this->k() * $value + $this->thicknes * cos($this->elevation) * (.5 + (.5 + $ser_nr) * $this->overrun),
																					$this->left + $this->barStep() * ($ord_nr +  $this->margin) - $this->thicknes * sin($this->elevation) * (-.5 + (.5 + $ser_nr) * $this->overrun), $this->zero() - $this->k() * $value + $this->thicknes * cos($this->elevation) * (-.5 + (.5 + $ser_nr) * $this->overrun),
														 ),
													4, $color);
		}
	}

	function ang2index($angle)
	{	$sum = 0;
		$counter = 0;
		if($this->data_sum)
		foreach ($this->agg_source as  $value)
		{	$sum += $value;
			if($sum / $this->data_sum * 2 * pi() > $angle)
			{	break;
			}
			$counter++;
		}
		return $counter % $this->colors_count;
	}
	function BriCon($color, $bri, $con)//
	{
		$r = ($color >> 16) & 0xFF;
		$g = ($color >> 8) & 0xFF;
		$b = $color & 0xFF;
		$r = 128 + ($r-128)*$con + $bri;
		if ($r<0) $r=0;
		if ($r>255) $r=255;
		$g = 128 + ($g-128)*$con + $bri;
		if ($g<0) $g=0;
		if ($g>255) $g=255;
		$b = 128 + ($b-128)*$con + $bri;
		if ($b<0) $b=0;
		if ($b>255) $b=255;
		return imagecolorallocate($this->im, $r, $g, $b);
	}
}

?>